//
//  SocketAckManager.swift
//  QHSocketIODemo
//
//  Created by Anakin chen on 2017/5/21.
//  Copyright © 2017年 chen. All rights reserved.
//

import Foundation

private struct SocketAck: Hashable {
    let ack: Int
    var callback: AckCallback!
    var hashValue: Int {
        return ack.hashValue
    }
    
    init(ack: Int) {
        self.ack = ack
    }
    
    init(ack: Int, callback: @escaping AckCallback) {
        self.ack = ack
        self.callback = callback
    }
}

private func <(lhs: SocketAck, rhs: SocketAck) -> Bool {
    return lhs.ack < rhs.ack
}

private func ==(lhs: SocketAck, rhs: SocketAck) -> Bool {
    return lhs.ack == rhs.ack
}

struct SocketAckManager {
    private var acks = Set<SocketAck>(minimumCapacity: 1)
    private let ackSemaphore = DispatchSemaphore(value: 1)
    
    mutating func addAck(_ ack: Int, callback: @escaping AckCallback) {
        acks.insert(SocketAck(ack: ack, callback: callback))
    }
}
